﻿var mfunction = {
    //表格初始化
    gridInit: function () {
        $("#mfunction-grid").datagrid({
            url: "/manage/function/grid",
            nowrap: false,
            rownumbers: true,
            border: false,
            fit: true,
            singleSelect: true,
            pageSize: 20,
            pagination: true,
            idField: "ID",
            toolbar: "#mfunction-tools",
            columns:
            [[
                {
                    field: "Icon", title: "图标", width: 50, align: "center",
                    formatter: function (value, row, index) {
                        if (value) { return manage.icon(value); }
                    }
                },
                { field: "Name", title: "名称", width: 80, align: "center" },
                { field: "Type", title: "类型", width: 80, align: "center" },
                { field: "Sequence", title: "排序", width: 50, align: "center" },
                { field: "Func", title: "功能", width: 100, align: "center" },
                { field: "Purview", title: "权限", width: 100, align: "center" },
                { field: "Remark", title: "描述", width: 280, align: "center" }
            ]]
        });
    },

    //添加
    insert: function () {
        mfunction.mutual("添加", null);
    },
    //修改
    update: function () {
        var row = $("#mfunction-grid").datagrid("getSelected");
        if (row) {
            mfunction.mutual("修改", row.ID);
        }
        else { manage.slide(manage.nullRowMsg); }
    },
    //删除
    remove: function () {
        var row = $("#mfunction-grid").datagrid("getSelected");
        if (row) {
            manage.confirm("确认删除?", function (del) {
                if (del) {
                    manage.ajax({
                        url: "/manage/function/remove",
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            manage.slide(result.message);
                            if (result.success) { mfunction.reload(); }
                        }
                    });
                }
            });
        }
        else { manage.slide(manage.delNullRowMsg); }
    },
    //实体操作
    mutual: function (title, id) {
        var href = "/manage/function/page?" + $.param({ id: id });
        var d = manage.dialog({
            title: title, href: href, width: 500,
            handler: function () {
                manage.form({
                    dialog: d, formid: "#mfunction-form",
                    url: "/manage/function/save",
                    success: function (result) { mfunction.reload(); }
                });
            },
            onLoad: function () { mfunction.formInit(); }
        });
    },
    //弹窗初始化
    formInit: function () {
        $("#mfunction-icon").textbox({
            required: true,
            icons: [{
                iconCls: "icon-application-view-icons",
                handler: function () {
                    var d = manage.dialog({
                        title: "选择图标",
                        id: "mfunction-iconbox",
                        width: 542, height: 460,
                        iconCls: "icon-bricks",
                        href: "/content/easyui/extensions/icons/select.html",
                        onLoad: function () {
                            $("span.list-span", "#mfunction-iconbox").click(function () {
                                var value = $(this).data("value");
                                d.dialog("close");
                                $("#mfunction-icon").textbox("setValue", value);
                            });
                        }
                    });
                }
            }]
        });
    },

    //排序上移
    desc: function () {
        mfunction.order("desc");
    },
    //排序下移
    asc: function () {
        mfunction.order("asc");
    },
    //排序操作
    order: function (order) {
        var row = $("#mfunction-grid").datagrid("getSelected");
        if (row) {
            manage.ajax({
                url: "/manage/function/setorder",
                type: "post", data: { id: row.ID, order: order },
                success: function (result) {
                    manage.slide(result.message);
                    if (result.success) {
                        $("#mfunction-grid").datagrid("reload");
                    }
                }
            });
        }
        else { manage.slide(manage.nullRowMsg); }
    },

    //刷新列表
    reload: function () {
        $("#mfunction-grid").datagrid("unselectAll");
        $("#mfunction-grid").datagrid("reload");
    }
};

$(function () { mfunction.gridInit(); });